package com.shengqi.xinnongren.common.base;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.Nullable;

import com.shengqi.xinnongren.work.dialog.LoadingDialog;

/**
 * MVP Activity 基类
 * Created by xuyougen on 2018/4/11.
 */

public abstract class BaseMvpActivity<V extends BaseView, P extends BasePresenter<V>> extends BaseActivity {

    protected P mPresenter;
    //加载时需要的dialog
    private Dialog mLoadingDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (mLoadingDialog == null)
            mLoadingDialog = createProgressDialog();
        if (mPresenter == null) {
            mPresenter = createPresenter();
            mPresenter.attach((V) this);
        }
        //开始进行初始化相关数据
//        initView();
//        initData();
    }

    protected Dialog createProgressDialog() {
        Dialog dialog = new LoadingDialog.Builder(this).build();
        dialog.setCancelable(false);
        return dialog;
    }


    @Override
    public void initPresenter() {
        mPresenter = createPresenter();
        mPresenter.attach((V) this);
        mLoadingDialog = createProgressDialog();
    }

    protected abstract P createPresenter();

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mPresenter.detach();
    }

    public void showLoadingDialog() {
        if (!mLoadingDialog.isShowing()) {
            mLoadingDialog.show();
        }
    }

    public void hideLoadingDialog() {
        if (mLoadingDialog.isShowing()) {
            mLoadingDialog.dismiss();
        }
    }
}
